﻿//2367. 等差三元组的数目
//给你一个下标从 0 开始、严格递增 的整数数组 nums 和一个正整数 diff 。如果满足下述全部条件，则三元组(i, j, k) 就是一个 等差三元组 ：
//i < j < k ，nums[j] - nums[i] == diff 且nums[k] - nums[j] == diff 返回不同 等差三元组 的数目。



class Solution {
public:
    int arithmeticTriplets(vector<int>& nums, int diff)
    {
        int j = 0, k = 0;
        int n = nums.size();
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            //第一个大于等于nums[i]+diff的元素
            while (j < n && nums[j] < nums[i] + diff)
            {
                j++;
            }
            while (k < n && nums[k] < nums[i] + 2 * diff)
            {
                k++;
            }
            if (j < n && k < n && nums[j] == nums[i] + diff && nums[k] == nums[i] + 2 * diff)
            {
                ans++;
            }
        }
        return ans;
    }
};